using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Pong_Xvna
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteFont font;
        SpriteBatch spriteBatch;
        Input input = new Input();

        Pad player1;
        AIPad player2;
        Ball ball;
        RedCircle redCircle;
        SpriteManager spriteManager;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 500;
        }

        protected override void Initialize()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            Services.AddService(typeof(Input), input);
            font = Content.Load<SpriteFont>("font");
            
            spriteManager = new SpriteManager(this);

            redCircle = (RedCircle)spriteManager.ProductUnit("RedCircle");

            player1 = (Pad)spriteManager.ProductUnit("Pad");

            player2 = (AIPad)spriteManager.ProductUnit("AIPad");

            ball = (Ball)spriteManager.ProductUnit("Ball");          

            base.Initialize();
        }

        

        protected override void LoadContent()
        {
                    
        }
        protected override void Update(GameTime gameTime)
        {
            input.Update();
            if (player1.CheckCollides(ball))
            {
                ball.svs = new SlowState();
                ball.VelocityX();
                ball.Velocity = new Vector2(ball.Velocity.X * -1, Math.Abs(ball.Center.Y - player1.Center.Y) / 5);
                
            }
            if (player2.CheckCollides(ball))
            {
                ball.svs = new SlowState();
                ball.VelocityX();
                ball.Velocity = new Vector2(ball.Velocity.X * -1, Math.Abs(ball.Center.Y - player2.Center.Y) / 5);
                
            }
            if (ball.CheckCollides(redCircle))
            {
                ball.svs = new FastState();
                ball.VelocityX();
            }
            if (ball.Position.X < 0)
            {
                player2.Point++;
                GameManager.Instance.Restart(player1, player2, ball);
            }
           if(ball.Position.X > 760)
           {
               player1.Point++;
               GameManager.Instance.Restart(player1, player2, ball);
           }

           if (player2.Center.Y < ball.Center.Y)
           {
               player2.Velocity = new Vector2(0, 4);
           }
           else
           {
               player2.Velocity = new Vector2(0, -4);
           }

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CadetBlue);

            spriteBatch.Begin(SpriteBlendMode.AlphaBlend);

            base.Draw(gameTime);

            spriteBatch.DrawString(font, "Player 1: " + player1.Point, new Vector2(100, 100), Color.White);
            spriteBatch.DrawString(font, "Player 2: " + player2.Point, new Vector2(600, 100), Color.White);
            
            if (player1.Point > 4)
            {
                spriteBatch.DrawString(font, "Player 1 win!", new Vector2(320, 200), Color.White);
                Components.Clear();
            }
            else
                if (player2.Point > 4)
                {
                    spriteBatch.DrawString(font, "Player 2 win!", new Vector2(320, 200), Color.White);
                    Components.Clear();
                }

            spriteBatch.End();
        }
    }
}
